package com.benyanyi.image_grid_lib;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author mylove
 * @date 2021/9/27
 * @email ben@yanyi.red
 * @overview
 */
class ImagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<String> mData = new ArrayList<>();
    private int mMaxLength = 0;//列表最大长度
    private boolean isAdd = false;//是否需要添加图片操作
    private int mMinShowSize = 0;//最小显示个数
    private boolean mShowDelete = false;//是否需要删除图片
    private int mDeletePadding = 0;//删除图标内边距
    private int mGravity = 0;//默认位于右边

    private int mItemRadius = 0;//圆角

    private final int NONE_VIEW = 0;
    private final int FOOTER_TYPE = 1;

    private ImageRecycler.OnAddImgClick onAddImgClick;
    private ImageRecycler.OnRemoveClick onRemoveClick;

    public ImagerAdapter(int mMaxLength, boolean isAdd, int mMinShowSize, boolean mShowDelete, int mDeletePadding, int mGravity, int mItemRadius) {
        this.mMaxLength = mMaxLength;
        this.isAdd = isAdd;
        this.mMinShowSize = mMinShowSize;
        this.mShowDelete = mShowDelete;
        this.mDeletePadding = mDeletePadding;
        this.mGravity = mGravity;
        this.mItemRadius = mItemRadius;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.benyanyi_item_img, parent, false);
        if (viewType == FOOTER_TYPE) {
            return new FooterViewHolder(view);
        } else {
            return new ViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            if (mShowDelete) {
                ImageView imageView = new ImageView(holder.itemView.getContext());
                imageView.setImageDrawable(ImageRecycler.mDeleteRes);
                imageView.setPadding(mDeletePadding, mDeletePadding, mDeletePadding, mDeletePadding);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                switch (mGravity) {
                    case 1:
                        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
                    case 2:
                        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                        break;
                    case 3:
                        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
                        break;
                    case 4:
                        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
                        break;
                    default:
                        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
                        break;
                }
                ((ViewHolder) holder).itemRelative.addView(imageView, layoutParams);
                imageView.setOnClickListener(view -> {
                    if (this.onRemoveClick != null) {
                        onRemoveClick.onRemoveClick(position);
                    }
                    remove(position);
                });
            }
            GlideUtils.loadImg(((ViewHolder) holder).img, mData.get(position), mItemRadius);
            ((ViewHolder) holder).img.setOnClickListener(view -> {
                ImageDetailsActivity.start(holder.itemView.getContext(), mData, position);
            });
        } else if (holder instanceof FooterViewHolder) {
            //                holder.itemView.setVisibility(View.GONE);
            //                holder.itemView.setVisibility(View.VISIBLE);
            setVisibility(position < mMaxLength, ((FooterViewHolder) holder).itemRelative);
            if (ImageRecycler.mAddRes != null) {
                ((FooterViewHolder) holder).img.setImageDrawable(ImageRecycler.mAddRes);
            }
            ((FooterViewHolder) holder).itemRelative.setOnClickListener(view -> {
                if (onAddImgClick != null) {
                    onAddImgClick.onAddImgClick();
                }
            });
        }
    }

    private void setVisibility(boolean isVisible, ViewGroup itemView) {
        ViewGroup.LayoutParams param = (ViewGroup.LayoutParams) itemView.getLayoutParams();
        if (isVisible) {
            param.height = LinearLayout.LayoutParams.MATCH_PARENT;
            param.width = LinearLayout.LayoutParams.MATCH_PARENT;
            itemView.setVisibility(View.VISIBLE);
        } else {
            itemView.setVisibility(View.GONE);
            param.height = 0;
            param.width = 0;
        }
        itemView.setLayoutParams(param);
    }

    public void setOnAddImgClick(ImageRecycler.OnAddImgClick onAddImgClick) {
        this.onAddImgClick = onAddImgClick;
    }

    public void setOnRemoveClick(ImageRecycler.OnRemoveClick onRemoveClick) {
        this.onRemoveClick = onRemoveClick;
    }

    @Override
    public int getItemCount() {
        int size = 0;
        if (isAdd) {
            if (mMinShowSize > 0) {
                size = mMinShowSize;
            } else {
                size += 1;
            }
        }
        if (mMaxLength <= 0 || mData.size() <= mMaxLength) {
            size += mData.size();
        } else {
            size += mMaxLength;
        }
        return size;
    }

    @Override
    public int getItemViewType(int position) {
        if (isFooter(position)) {
            return FOOTER_TYPE;
        } else {
            return NONE_VIEW;
        }
    }

    private boolean isFooter(int position) {
        if (isAdd) {
            if (mMinShowSize > 0) {
                return position < getItemCount() && position >= getItemCount() - mMinShowSize;
            } else {
                return position < getItemCount() && position >= getItemCount() - 1;
            }
        } else {
            return false;
        }

    }

    public void addData(@IntRange(from = 0L) int position, @NonNull String data) {
        if (position >= this.mMaxLength) {
            return;
        }
        this.mData.add(position, data);
        this.notifyItemInserted(position);
        this.compatibilityDataSizeChanged(1);
    }

    public void addData(@NonNull String data) {
        this.mData.add(data);
        this.notifyItemInserted(this.mData.size());
        this.compatibilityDataSizeChanged(1);
    }

    public void remove(@IntRange(from = 0L) int position) {
        if (position >= this.mMaxLength) {
            return;
        }
        this.mData.remove(position);
        this.notifyItemRemoved(position);
        this.compatibilityDataSizeChanged(0);
        this.notifyItemRangeChanged(position, getItemCount() - position);
    }

    public void setData(@IntRange(from = 0L) int index, @NonNull String data) {
        if (index >= this.mMaxLength) {
            return;
        }
        this.mData.set(index, data);
        this.notifyItemChanged(index);
    }

    public void addData(@IntRange(from = 0L) int position, @NonNull Collection<? extends String> newData) {
        this.mData.addAll(position, newData);
        this.notifyItemRangeInserted(position, newData.size());
        this.compatibilityDataSizeChanged(newData.size());
    }

    public void addData(@NonNull Collection<? extends String> newData) {
        this.mData.addAll(newData);
        this.notifyItemRangeInserted(this.mData.size() - newData.size(), newData.size());
        this.compatibilityDataSizeChanged(newData.size());
    }

    public void replaceData(@NonNull Collection<? extends String> data) {
        if (data != this.mData) {
            this.mData.clear();
            this.mData.addAll(data);
        }
        this.notifyDataSetChanged();
    }

    private void compatibilityDataSizeChanged(int size) {
        int dataSize = this.mData == null ? 0 : this.mData.size();
        if (dataSize == size) {
            this.notifyDataSetChanged();
        }
    }

    @NonNull
    public List<String> getData() {
        return this.mData;
    }

    @Nullable
    public String getItem(@IntRange(from = 0L) int position) {
        return position >= 0 && position < this.mData.size() ? this.mData.get(position) : null;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private SquareRelativeLayout itemRelative;
        private ImageView img;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            init(itemView);
        }

        private void init(View itemView) {
            itemRelative = itemView.findViewById(R.id.item_item);
            img = itemView.findViewById(R.id.item_img);
        }


    }

    static class FooterViewHolder extends RecyclerView.ViewHolder {

        private SquareRelativeLayout itemRelative;
        private ImageView img;

        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);
            init(itemView);
        }

        private void init(View itemView) {
            itemRelative = itemView.findViewById(R.id.item_item);
            img = itemView.findViewById(R.id.item_img);
        }

    }

}
